package com.daylightrobbery.controller;

import com.daylightrobbery.entity.Account;
import com.daylightrobbery.entity.LoginReq;
import com.daylightrobbery.entity.LoginResp;
import com.daylightrobbery.entity.Result;
import com.daylightrobbery.server.GameManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/api/account")
public class AccountController {
    private static final Logger log = LoggerFactory.getLogger(AccountController.class);

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public Result<LoginResp> login(@RequestBody LoginReq req){
        LoginResp resp = new LoginResp();
        resp.setUid(UUID.randomUUID().toString().substring(0, 8));
        resp.setAccount(req.getAccount());
        GameManager.getInstance().addUserOnline(
                resp.getUid(),
                new Account().setUid(resp.getUid()).setAccount(resp.getAccount()));
        return Result.success(resp);
    }

}
